﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class cart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.IsPostBack) return;

        if (Session["number"] == null || Session["number"].ToString() == "0")
        {
            Response.Write("<script>alert('Giỏ hàng hiện không có sản phẩm nào!!!');</script>");
            Server.Transfer("Default.aspx");
        }

        string cart = Session["cart"].ToString();
        ItemBLL it = new ItemBLL();
        CartItems.DataSource = it.getItemInCart(cart);
        CartItems.DataBind();
    }
    protected void CartItems_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "addQuanlity")
        {
            TextBox txt = (TextBox)e.Item.FindControl("ItemId");
            int num;

            if (!Int32.TryParse(txt.Text, out num))
                Response.Redirect("Cart.aspx");
            else if (num > 3)
            {
                Response.Write("<script>alert('Mỗi sản phẩm không được mua quá số lượng 3!!!');</script>");
                Server.Transfer("Cart.aspx");
            }
            else
            {
                Session["item" + e.CommandArgument] = num;
                Response.Redirect("Cart.aspx");
            }
        }
        else if (e.CommandName == "delItem")
        {
            string[] cart = Session["cart"].ToString().Split(',');
            string temp = "";
            for (int i = 0; i < cart.Length - 1; i++)
            {
                if (cart[i] != e.CommandArgument.ToString())
                    temp += cart[i] + ",";
            }

            Session["item" + e.CommandArgument] = null;
            Session["cart"] = temp;
            Session["number"] = Int32.Parse(Session["number"].ToString()) - 1;
            Server.Transfer("Cart.aspx");
        }
    }
    protected int getTotalCash()
    {
        int totalCash = 0;
        string[] cart = Session["cart"].ToString().Split(',');
        ItemBLL it = new ItemBLL();

        int num = 0;
        for (int i = 0; i < cart.Length - 1; i++)
        {
            num = Int32.Parse(Session["item" + cart[i]].ToString());
            totalCash += it.getCash(cart[i]) * num;
        }
        return totalCash;
    }
    protected int getSumCash(string id)
    {
        int num = Int32.Parse(Session["item" + id].ToString());
        ItemBLL it = new ItemBLL();
        return (it.getCash(id) * num);
    }

    protected void btnCheckout_Click(object sender, EventArgs e)
    {
        Response.Redirect("Checkout.aspx");
    }
    protected void btnDelCart_Click(object sender, EventArgs e)
    {
        Session["number"] = 0;
        string[] cart = Session["cart"].ToString().Split(',');

        for (int i = 0; i < cart.Length - 1; i++)
        {
            Session["item" + cart[i]] = null;
        }
        Session["cart"] = null;
        Response.Redirect("Default.aspx");
    }
}